import pickle
import os


class test_kernal:
	#默认构造函数
	def __init__(self,number1=1,grades1=list(-1 for i in range(240))):
		self.number=number1
		self.grades=grades1
		self.question_list=list(-1 for i in range(241))
		self.question_not_answered=set(list(range(1,241)))
		self.gender = 0


		self.small_grades=list(-1 for i in range(30))
		self.small_title=list(-1 for i in range(30)) 
		self.real_small_grades=list(-1 for i in range(30))

		self.large_grades=list(-1 for i in range(5))
		self.large_title=list(-1 for i in range(5))
		self.real_large_grades=list(-1 for i in range(30))


	#读取进度
	def readfile(self):
			file_dir = os.getcwd()
			recordgrade_name = os.path.join(file_dir, "record-grade.txt")  # 读取分数
			recordset_name = os.path.join(file_dir, "record-set.txt")  # 读取未答集合
			recordnumber_name = os.path.join(file_dir, "record-number.txt")  # 读取题号位置
			
			recordgrade = open(recordgrade_name,'rb')
			recordset = open(recordset_name,'rb')
			recordnumber = open(recordnumber_name,'rb')
			self.question_not_answered = pickle.load(recordset)
			self.grades = pickle.load(recordgrade)
			self.number = pickle.load(recordnumber)
			recordgrade.close()
			recordnumber.close()
			recordset.close()


		


	 # 删除文件
	def delfile(self):
		file_dir = os.getcwd()
		recordgradefile_name = os.path.join(file_dir, "record-grade.txt")
		recordnumberfile_name = os.path.join(file_dir, "record-number.txt")
		os.remove(recordgradefile_name)
		os.remove(recordnumberfile_name)
		os._exit(0)


	#创建一个列表，以题号为下标，对应题目文本
	def read_all_question(self):
		question_file = open("question.txt", 'r', encoding='UTF-8')
		for i in range(240):
			question_line = question_file.readline()
			question_index, question_text = question_line.split(' ')
			question_text = question_text.replace('\n', '')
			question_index=int(question_index.split('.')[0])
			self.question_list[question_index]=question_text



	# #展示题目，采集答案
	# def show_question_get_answer(self):
	# 	while self.number in list(range(1,241)):
	# 		print("0-4表示契合程度递增，a表示回到上一题,d跳到下一题,s保存并退出,j跳转题目,c查看进度")
	# 		bili=round((1-float(len(self.question_not_answered)/240)),2)
	# 		print('作答进度：%s%%'%bili)
	# 		bili=int(bili/5)
	# 		print('进度条：')
	# 		for i in range(bili):
	# 			print('⬛',end='')
	# 		for i in range(20-bili):
	# 			print('⬜',end='')
	# 		print()
	# 		if self.number in self.question_not_answered: 
	# 			print('本题状态：未答')
	# 		else:
	# 			print('本题状态：已答\n')
	# 			print('你之前的答案是%s'%self.grades[self.number-1])	
	# 		s=input(str(self.number)+'-'+self.question_list[self.number]+'\n'+'符合程度（或你选择的操作）：')
	# 		while s not in ['1','2','3','4','0','a','d','s','s保存并退出','j','c']: 
	# 			s=input('不合法，请重新输入：\n')
	# 		if s in ['1','2','3','4','0']:
	# 			self.grades[self.number-1]=int(s)
	# 			if self.number in self.question_not_answered: 
	# 				self.question_not_answered.remove(self.number)
	# 			self.number+=1
	# 		elif s=='s' or s=='s保存并退出':
	# 			self.savefile()
	# 		elif s=='j':
	# 			self.jump()
	# 		elif s=='a':
	# 			if self.number==1:
	# 				print('已经是第一题了！')
	# 				self.number+=1
	# 			self.number-=1
	# 		elif s=='c':
	# 			self.check_schedule()
	# 		elif s=='d':
	# 			if self.number==240:
	# 				print('已经是最后一道题了！')
	# 				self.number-=1
	# 			self.number+=1
	# 		print()
	# 		print('--------------------------------------------------')
	# 		self.jugde_end()


	# #查看进度
	# def check_schedule(self):
	# 	for i in range(1,241):
	# 		if i not in self.question_not_answered:
	# 			print('⬛',end='')
	# 		else:
	# 			print('⬜',end='')


	# #判断是否结束
	# def jugde_end(self):
	# 	if  self.number==241:
	# 		if self.question_not_answered==set():
	# 			jugde_s=''
	# 			while not( jugde_s in ['y','n']):
	# 				jugde_s=input('你已经答完所有题了，是否结束作答？\
	# 					\n y-结束作答，查看结果\n n-继续作答\n')
	# 			if jugde_s=='y':
	# 				self.end()
	# 			else:
	# 				self.jump()
	# 		else:
	# 			print('以下题目还未作答：\n')
	# 			for i in self.question_not_answered:
	# 				print(i,end=' , ')
	# 			print()
	# 			print('请补充作答\n')
	# 			self.jump()
	# 	if self.question_not_answered==set():
	# 		jugde_s=''
	# 		while not( jugde_s in ['y','n']):
	# 			jugde_s=input('你已经答完所有题了，是否结束作答？\
	# 					\n y-结束作答，查看结果\n n-继续作答')
	# 		if jugde_s=='y':
	# 			self.end()
	# 		else:
	# 			self.jump()



	# 保存文件并退出
	def savefile(self):   
		file_dir = os.getcwd()
		recordgrade_name = os.path.join(file_dir, "record-grade.txt")  # 保存分数
		recordnumber_name = os.path.join(file_dir, "record-number.txt")  # 保存题号位置
		recordset_name = os.path.join(file_dir, "record-set.txt")  # 保存未答题号
		recordgrade = open(recordgrade_name,'wb')
		recordnumber = open(recordnumber_name,'wb')
		recordset = open(recordset_name,'wb')
		pickle.dump(self.question_not_answered,recordset)
		pickle.dump(self.grades,recordgrade)
		pickle.dump(self.number,recordnumber)
		recordgrade.close()
		recordnumber.close()
		recordset.close()
		os._exit(0)



	#结束阶段
	def end(self):
		self.calculate()
		self.show_points()
		s=input('回车结束程序\n')
		os._exit(0)
		#self.show_analysis()
	

	#计算各项分数
	def calculate(self):
		self.read_inverse_questions()
		self.get_small_grades()
		self.get_large_grades()
		self.get_real_grades()
		self.apart_points_list()


	#读取反向分数的题目，形成集合,并将对应分数取为负数
	def read_inverse_questions(self):
		inverse_set=set()
		inverse_question_file = open("reverse-index.txt", 'r', encoding='UTF-8')
		for i in range(103):
			re_index_line=inverse_question_file.readline()
			re_index_number=eval(re_index_line.split(' ')[0])
			inverse_set.add(re_index_number)
		for i in inverse_set:
			self.grades[i-1]=4-self.grades[i-1]


	#读取各小维度对应小题号，计算各小维度小题分
	def get_small_grades(self):
		small_index=list(range(30))
		small_grades_file=open("all-index.txt",'r',encoding='UTF-8')
		for i in range(30):
			p=small_grades_file.readline()
			self.small_title[i]=p.split('=')[0]
			small_title_letter=self.small_title[i][2]
			small_title_number=self.small_title[i][3]
			self.small_title[i]=small_title_letter+small_title_number
			st=small_grades_file.readline()
			small_index[i]=st.split('.')
			self.small_grades[i]=0
			for small_index1 in small_index[i]:
				self.small_grades[i]+=self.grades[int(small_index1)-1]



	#计算各大维度总分数
	def get_large_grades(self):
		for i in range(5):
			self.large_grades[i]=0
		for i in range(5):
			for j in range(6):
				self.large_grades[i]+=self.small_grades[6*i+j]
			self.large_title[i]=self.small_title[6*i][0]





	def get_real_grades(self):
		if self.gender == 'woman':
			self.get_real_grades_woman()
		elif self.gender == 'man':
			self.get_real_grades_man()

	#计算各大项、小项真实得分(男)
	def get_real_grades_man(self):
		really_grade = []
		f8_sum_list = self.large_grades+self.small_grades
		man_M = [83.78, 109.96, 110.64, 113, 123.88, 14.15, 13.82, 14.37, 15.35, 14.86, 11.23,
			21.53, 18.69, 15.1, 18.33, 17.24, 19.07, 15.19,
			20.7, 19.47, 15.02, 19.62, 20.64, 21.07, 19.03, 20.99, 15.33, 16.67, 19.91,
			20.8, 18.39, 23.47, 20.02, 20.48, 20.72]

		man_SD = [24.41, 18.27, 15.88, 14.9, 20.32, 5.7, 5.24, 5.7, 4.73, 4.88,
			  5.09, 4.27, 4.65, 4.37, 4.74, 4.64, 4.85,
			  4.19, 4.88, 3.95, 3.61, 4.98, 3.97,
			  3.94, 5.11, 3.53, 4.64, 4.06, 3.59,
			  4.34, 4.21, 4.22, 4.34, 4.66, 4.63]
		for i in range(35):
			if f8_sum_list[i] < (man_M[i] - 2 * man_SD[i]):
				really_grade.append(1)
			elif (f8_sum_list[i] >= man_M[i] - 2 * man_SD[i]) and (f8_sum_list[i] <= man_M[i] - 1.5 * man_SD[i]):
				really_grade.append(2)
			elif ((f8_sum_list[i] > man_M[i] - 1.5 * man_SD[i])) and (f8_sum_list[i] <= man_M[i] - man_SD[i]):
				really_grade.append(3)
			elif (f8_sum_list[i] > man_M[i] - man_SD[i]) and (f8_sum_list[i] <= man_M[i] - 0.5 * man_SD[i]):
				really_grade.append(4)
			elif (f8_sum_list[i] > man_M[i] - 0.5 * man_SD[i]) and (f8_sum_list[i] <= man_M[i]):
				really_grade.append(5)
			elif (f8_sum_list[i] > man_M[i]) and (f8_sum_list[i] <= man_M[i] + 0.3 * man_SD[i]):
				really_grade.append(6)
			elif (f8_sum_list[i] > man_M[i] + 0.3 * man_SD[i]) and (f8_sum_list[i] <= man_M[i] + 0.6 * man_SD[i]):
				really_grade.append(7)
			elif (f8_sum_list[i] > man_M[i] + 0.6 * man_SD[i]) and (f8_sum_list[i] <= man_M[i] + man_SD[i]):
				really_grade.append(8)
			elif (f8_sum_list[i] > man_M[i] + man_SD[i]) and (f8_sum_list[i] <= man_M[i] + 1.5 * man_SD[i]):
				really_grade.append(9)
			elif (f8_sum_list[i] > man_M[i] + 1.5 * man_SD[i]):
				really_grade.append(10)
		for i in range(5):
			self.real_large_grades[i]=really_grade[i]
		for i in range(30):
			self.real_small_grades[i]=really_grade[i+5]


	#计算各大项、小项真实得分(女)
	def get_real_grades_woman(self):
		really_grade = []
		f8_sum_list = self.large_grades+self.small_grades
		man_M = [87.26,106.14,109.31,119,125.07,14.81,13.99,14.46,16.38,14.89,12.73,22.05,18.3,14.59,18.01,14.25,
18.94,15.89,20.45,19.59,15.02,17.68,20.68,21.84,21.54,21.19,16.19,17.68,20.56,20.44,19.32,24.18,
19.69,21.01,20.43]

		man_SD = [24.57,18.85,17.26,15.79,17.67,5.45,5.16,5.45,5.27,5.1,5.18,4.31,4.85,4.67,4.93,5.35,5.09,4.78,4.78,
4.32,3.88,5.33,3.76,4.43,4.97,3.88,4.72,4.62,3.76,4.33,4.75,3.48,4.07,4.03,4.66]
		for i in range(35):
			if f8_sum_list[i] < (man_M[i] - 2 * man_SD[i]):
				really_grade.append(1)
			elif (f8_sum_list[i] >= man_M[i] - 2 * man_SD[i]) and (f8_sum_list[i] <= man_M[i] - 1.5 * man_SD[i]):
				really_grade.append(2)
			elif ((f8_sum_list[i] > man_M[i] - 1.5 * man_SD[i])) and (f8_sum_list[i] <= man_M[i] - man_SD[i]):
				really_grade.append(3)
			elif (f8_sum_list[i] > man_M[i] - man_SD[i]) and (f8_sum_list[i] <= man_M[i] - 0.5 * man_SD[i]):
				really_grade.append(4)
			elif (f8_sum_list[i] > man_M[i] - 0.5 * man_SD[i]) and (f8_sum_list[i] <= man_M[i]):
				really_grade.append(5)
			elif (f8_sum_list[i] > man_M[i]) and (f8_sum_list[i] <= man_M[i] + 0.3 * man_SD[i]):
				really_grade.append(6)
			elif (f8_sum_list[i] > man_M[i] + 0.3 * man_SD[i]) and (f8_sum_list[i] <= man_M[i] + 0.6 * man_SD[i]):
				really_grade.append(7)
			elif (f8_sum_list[i] > man_M[i] + 0.6 * man_SD[i]) and (f8_sum_list[i] <= man_M[i] + man_SD[i]):
				really_grade.append(8)
			elif (f8_sum_list[i] > man_M[i] + man_SD[i]) and (f8_sum_list[i] <= man_M[i] + 1.5 * man_SD[i]):
				really_grade.append(9)
			elif (f8_sum_list[i] > man_M[i] + 1.5 * man_SD[i]):
				really_grade.append(10)
		for i in range(5):
			self.real_large_grades[i]=really_grade[i]
		for i in range(30):
			self.real_small_grades[i]=really_grade[i+5]




	def apart_points_list(self):
		self.apart_points = list(range(5))
		for i in list(range(5)):
			self.apart_points[i] = list(range(7))
		for i in list(range(5)):
			self.apart_points[i][0] = self.real_large_grades[i]
			for j in list(range(1,7)):
				self.apart_points[i][j] = self.real_small_grades[6*i+j-1]


	#展示各项得分
	def show_points(self):
		self.show_real_small_analysis()
		self.show_real_large_analysis()


	#展示各小项得分
	def show_real_small_analysis(self):
		for i in range(30):
			print("%s小项目得分="%self.small_title[i])
			print(self.real_small_grades[i])
	

	#展示各大项得分
	def show_real_large_analysis(self):
		for i in range(5):
			print("%s大项得分="%self.large_title[i])
			print(self.real_large_grades[i])


	#def show_analysis(self):
		#print('特别的高分或者低分分析')